package com.cptravel.activity.explore;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.core.api.ApiListenrDefaultImpl;
import com.core.api.B5MBaseResponse;
import com.core.controller.B5MBaseFragment;
import com.core.util.B5MActivityHelper;
import com.core.util.B5MDisplayHelper;
import com.core.widget.pulltorefresh.PullToRefreshBase.Mode;
import com.core.widget.pulltorefresh.PullToRefreshGridView;
import com.cptravel.R;
import com.cptravel.adapter.ExploreAdapter;
import com.cptravel.api.GetserveltclassRequest;
import com.cptravel.api.GetserveltclassResponse;
import com.cptravel.bean.Class;

import java.util.ArrayList;

public class ExploreFragment extends B5MBaseFragment implements OnClickListener
{
    private PullToRefreshGridView mPullToRefreshGridView;
    private ExploreAdapter mExploreAdapter;
    private GetserveltclassRequest mGetserveltclassRequest;

    @Override public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.explore;
    }

    @Override public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        bt_left = (Button) containerView.findViewById(R.id.bt_head_left);
        bt_left.setVisibility(View.GONE);
        bt_right = (Button) containerView.findViewById(R.id.bt_head_right);
        tv_title = (TextView) containerView.findViewById(R.id.tv_head_title);
        tv_title.setText("探索");
        bt_right.setVisibility(View.VISIBLE);
        bt_right.setOnClickListener(this);
        mProgressBar = (ProgressBar) containerView.findViewById(R.id.pb_head);
        mPullToRefreshGridView = (PullToRefreshGridView) containerView.findViewById(R.id.pull_refresh_grid);
        int paddingValue = B5MDisplayHelper.dip2px(this.getActivity(), 5);
        mPullToRefreshGridView.getRefreshableView().setPadding(paddingValue, paddingValue, paddingValue, paddingValue);
        mPullToRefreshGridView.getRefreshableView().setVerticalSpacing(paddingValue);
        mPullToRefreshGridView.getRefreshableView().setHorizontalSpacing(paddingValue);
        mPullToRefreshGridView.setMode(Mode.DISABLED);
        mPullToRefreshGridView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                // TODO Auto-generated method stub
                Class class1 = mExploreAdapter.getList().get((int) arg3);
                Bundle bundle = new Bundle();
                bundle.putSerializable("class1", class1);
                B5MActivityHelper.startActivity(mActivity, ExploreItemActivity.class, bundle);
            }
        });
    }

    @Override public void doInitDataes()
    {
        // TODO Auto-generated method stub
        mExploreAdapter = new ExploreAdapter(mActivity, new ArrayList<Class>());
        mPullToRefreshGridView.setAdapter(mExploreAdapter);
        mGetserveltclassRequest = new GetserveltclassRequest(new ApiListenrDefaultImpl()
        {
            @Override public void onSuccess(B5MBaseResponse b5mBaseResponse)
            {
                // TODO Auto-generated method stub
                super.onSuccess(b5mBaseResponse);
                GetserveltclassResponse getserveltclassResponse = (GetserveltclassResponse) b5mBaseResponse;
                mExploreAdapter.getList().clear();
                mExploreAdapter.setList(getserveltclassResponse.classes);
                mExploreAdapter.notifyDataSetChanged();
            }
        }, mProgressBar);
    }

    @Override public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    @Override public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        if (mExploreAdapter.getList().isEmpty() && !mGetserveltclassRequest.isStarted())
        {
            mGetserveltclassRequest.start();
        }
    }

    @Override public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.bt_head_right:
                B5MActivityHelper.startActivity(mActivity, SearchCustomActivity.class);
                break;
            default:
                break;
        }
    }
}
